package com.jianbing.zuixia.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.core.domain.model.LoginUser;
import com.jianbing.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.zuixia.domain.ZuixiaCoins;
import com.jianbing.zuixia.service.IZuixiaCoinsService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 最下金币记录Controller
 * 
 * @author wyj
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/zaixia/coins")
public class ZuixiaCoinsController extends BaseController
{
    @Autowired
    private IZuixiaCoinsService zuixiaCoinsService;

    /**
     * 查询最下金币记录列表
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZuixiaCoins zuixiaCoins)
    {
        startPage();
        List<ZuixiaCoins> list = zuixiaCoinsService.selectZuixiaCoinsList(zuixiaCoins);
        return getDataTable(list);
    }

    /**
     * 导出最下金币记录列表
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:export')")
//    @Log(title = "最下金币记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZuixiaCoins zuixiaCoins)
    {
        List<ZuixiaCoins> list = zuixiaCoinsService.selectZuixiaCoinsList(zuixiaCoins);
        ExcelUtil<ZuixiaCoins> util = new ExcelUtil<ZuixiaCoins>(ZuixiaCoins.class);
        util.exportExcel(response, list, "最下金币记录数据");
    }

    /**
     * 获取最下金币记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zuixiaCoinsService.selectZuixiaCoinsById(id));
    }

    /**
     * 新增最下金币记录
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:add')")
//    @Log(title = "最下金币记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZuixiaCoins zuixiaCoins)
    {
        zuixiaCoins.setUserid(getappid(SecurityUtils.getLoginUser()));
        return toAjax(zuixiaCoinsService.insertZuixiaCoins(zuixiaCoins));
    }

    /**
     * 修改最下金币记录
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:edit')")
//    @Log(title = "最下金币记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZuixiaCoins zuixiaCoins)
    {
        return toAjax(zuixiaCoinsService.updateZuixiaCoins(zuixiaCoins));
    }

    /**
     * 删除最下金币记录
     */
//    @PreAuthorize("@ss.hasPermi('zaixia:coins:remove')")
//    @Log(title = "最下金币记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zuixiaCoinsService.deleteZuixiaCoinsByIds(ids));
    }

     private Long getappid(LoginUser loginUser) {
        return loginUser.getUserId();
    }

    @GetMapping("/getCoins")
    public AjaxResult getCoins(){
      Long systemUserId = getappid(SecurityUtils.getLoginUser());
      ZuixiaCoins tempfilter = new ZuixiaCoins();
        tempfilter.setUserid(systemUserId);
        List<ZuixiaCoins> list = zuixiaCoinsService.selectZuixiaCoinsList(tempfilter);
        if(list.size() > 0){
            Long coins = 0L;
            for(ZuixiaCoins zuixiaCoins : list){
                coins += zuixiaCoins.getCoins();
            }
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("coins", coins);
            ajaxResult.put("list", list);
            return AjaxResult.success(ajaxResult);
        }else{
            Long coins = 0L;
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("coins", coins);
            ajaxResult.put("list", list);
            return AjaxResult.success(ajaxResult);
        }

    }
}
